package com.tarena.practice.manage.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tarena.practice.manage.pojo.Topic;
import com.tarena.practice.manage.service.TopicService;

@Controller
@RequestMapping("/topic/")
public class ExamController {
	
	@RequestMapping("user/exam")
	public String goExam(HttpServletRequest request) {
		findAllTopic(request);
		return "exam";
	}

	/*
	 * 测试访问tb_item_cat表获取所有数据 返回json字符串
	 */
	@Autowired
	private TopicService itemCatService;

	@RequestMapping("findAllTopic")
	@ResponseBody
	public List<Topic> findAllTopic(HttpServletRequest request) {
		List<Topic> topicList = itemCatService.findAllTopic();
		// for (Topic topic : topicList) {
		// System.out.println(topic.getTopic_content());
		// }
		List<Integer> num = new ArrayList<Integer>();
		num.add(999);
		int a = 0;
		List<Topic> list = new ArrayList<Topic>();
		for (int i = 0; i < 60; i++) {

			boolean flag = false;
			a = (int) (Math.random() * topicList.size());
			for (int n : num) {
				if (a == n) {
					flag = true;
					break;
				}
			}
			if (flag == false) {
				num.add(a);
				list.add(topicList.get(a));
				System.out.println("随机选出第" + a + "题作为本次的第" + (i + 1) + "题。");
				System.out.println("问题:" + list.get(i).getTopicContent());
			} else {
				i = i - 1;
			}
		}
		request.setAttribute("topicList", list);
		return list;
	}
}
